<?php
include_once "lib/config.php";
include_once "header.php";


if(isset($_POST['save'])&& $_POST['save']!="")
{
	$data= $_POST['data'];
	if(isset($_POST['help_id']) && $_POST['help_id']>0)
	{
	    $check_query=DB::updatecheckExistance("helps",array("help_question"=>$data['help_question']),array("help_id"=>$_POST['help_id']));
        
		if($check_query==0)
		{
            DB::updateData("helps",$data,array("help_id"=>$_POST['help_id']));
				
			if(DB::$affectedrows>0)
			{
			    
				$_SESSION['msg_success']="Help updated successfully.";
                redirect('helpmanager.php');
			}
			else
			{
				$msg_error="There is no record updated.";
			}
		}
		else
		{
			$msg_error="There is already one help record exists for given help question.";
		}
	}
	else
	{
		$check_query=DB::checkExistance("helps",array("help_id"=>$data['help_id']));
        
		if($check_query==0)
		{
            $data['status']=1;
			DB::insertData("helps",$data);
			if(DB::$affectedrows>0)
			{
			 
				$_SESSION['msg_success']="Help added successfully.";
				redirect('helpmanager.php');
			}
			else
			{
				$msg_error="There is no record added.";
			}
			
		}
		else
		{
			$msg_error="There is already one help record exists for given help question.";
		}
	}
	
}

if(isset($_POST['help_id']) && $_POST['help_id']>0)
{
	$conditionarr=array(":help_id"=>$_POST['help_id']);
    $datainfo=DB::fetch_array(DB::query("select * from helps where help_id=:help_id",$conditionarr));
    
    if(isset($datainfo[0]))
    {
        $data=$datainfo[0];
    }
    else
    {
        $data=array();
    }
    
    
}
$helpCatinfo=DB::fetch_array(DB::query("select * from help_categories "));
?>
<script src="Classes/ckeditor/ckeditor.js"></script>

  <link rel="stylesheet" href="css/jquery-ui.css" />
  <script src="js/jquery-1.9.1.js"></script>
  <script src="js/jquery-ui.js"></script>		
		<div class="container_12" id="main_div">
           <form action="" method="post" onsubmit="return validateaddedit();" enctype="multipart/form-data">
			<br>
			<div class="grid_12" >
		    	<?php
            	if(isset($msg_success) && $msg_success!="")
            	{
            	?>
                <div>
                    <span class="notification n-success"><?php echo $msg_success; ?></span>
                </div>
                <?php
                }
                if(isset($msg_error) && $msg_error!="")
                {
                ?>
                <span class="notification n-error"><?php echo $msg_error; ?></span>
                <?php
				}
                ?>
		        <div class="module" >
		        		
		             <h2><span onclick="toggle_div('basic_body','toogleimg');" ><img src="images/desc.gif" height="5px" id="toogleimg">&nbsp;&nbsp;Help Details</span></h2>
		                
		             <div class="module-body" id="basic_body">
		                
		                  	<p>
		                        <label>Help Category<span style="color: red;">*</span></label>
		                        <select class="input-short" id="help_cat_id" name="data[help_cat_id]" style="width: 280px;">
		                        	<option value="">Select Category</option>
		                        <?php
		                        	foreach($helpCatinfo as $helpcat)
		                        	{
								?>
									<option value="<?php echo $helpcat["help_cat_id"]; ?>" <?php if(isset($data['help_cat_id']) && $data['help_cat_id']!=Null){ if($data['help_cat_id']==$helpcat["help_cat_id"])echo "selected"; } ?>><?php echo $helpcat["help_cat_name"]; ?></option>
								<?php		
									}
		                        ?>
		                        </select>
		                    </p>
		                    <p>
		                        <label>Help Question<span style="color: red;">*</span></label>
		                        <textarea rows="7" cols="90" name="data[help_question]" id="help_question" class="input-short" style="width:280px;"><?php if(isset($data['help_question']) && $data['help_question']!=Null)echo $data['help_question']; ?></textarea>
		                    </p>
                            <p>
		                        <label>Help Answer<span style="color: red;">*</span></label>
		                        <textarea rows="7" cols="90" name="data[help_answer]" id="help_answer" ><?php if(isset($data['help_answer']) && $data['help_answer']!=Null)echo $data['help_answer']; ?></textarea>
		                    </p>
		             </div> <!-- End .module-body -->
		
		        </div>  <!-- End .module -->
				
		    </div>
		    
		    <div class="grid_12">
		    <fieldset>
		    	&nbsp;
		    	<input type="hidden" id="help_id" name="help_id" value="<?php if(isset($_POST['help_id'])){ echo $_POST['help_id']; } ?>">
		        <input class="submit-green" type="submit" name="save" value="Submit" /> 
		        <input class="submit-gray" type="button" value="Back" onclick="window.location='helpmanager.php'" />
		    </fieldset>
		    </div>
		    <div style="clear:both;"></div>
		 </form>    
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>           
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>
<script>
CKEDITOR.replace( 'help_answer' );
</script>
<script>

function validateaddedit()
{

	if(trim(document.getElementById("help_cat_id").value)=="")
	{
		alertify.alert("Help category field is required.");
		document.getElementById("help_cat_id").focus();
		return false;
	}
	else if(trim(document.getElementById("help_question").value)=="")
	{
		alertify.alert("Help question field is required.");
		document.getElementById("help_question").focus();
		return false;
	}
//    else if(document.getElementById("help_answer").value=="")
//	{
//		alertify.alert("Help answer field is required.");
//		document.getElementById("help_answer").focus();
//		return false;
//	}
}
</script>       